$(document).ready(function() {
  $('#message_form textarea').keyup(function(event){
    if ((event.ctrlKey) && (event.keyCode == 13)) {
      sendMessage();
    }
  })
  $('.message').each(function() {
    text = $(this).children('p:first').text();
    text = text.replace(/[\r\n]/g, '<br />');
    $(this).children('p:first').html(text);
  })
});

function getDate() {
	var month_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");	
	var d = new Date();
	var current_date = d.getDate();
	var current_month = d.getMonth();
	var current_year = d.getFullYear();
	
	return current_date + " " + month_names[current_month] 	+ " " + current_year;
}

function getTime() {
	var currentTime = new Date();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	
	if(minutes < 10) {
		minutes = "0" + minutes;
	}
	return hours + ":" + minutes;
}

function sendMessage() {
  text = $('#message_form textarea').val();
  name = $('#user_name').val();
  text = text.replace(/[\r\n]+$/, '');
  if(text != '') {
    $.post(addMessageUrl,
    {text: text},
    function(msg){
      text = text.replace(/[\r\n]/g, '<br />')
      message = '<div class="message"><p>' + text + '</p><p>' + name + ' - ' + getDate() + ' ' + getTime() + '</p></div>';    
      $('.message_box').append(message);
      $('#message_form textarea').val('');
    })
  }
  return false;
}